<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\controller\Index as Ic;
use think\Db;
use think\Request;
use think\facade\Env;
use think\facade\Config;

class Createdata extends Base
{
	protected $config;
	protected $c;
	protected $n;
	protected $no;
	protected $num;
	protected $nums;
	protected function initialize()
	{
	    parent::initialize();
	    $this->config = Db::name('system')->where('id',1)->find();
		$this->c = new Ic();
		$this->c->clear();
		Config::set('template.view_path','template/');
		Config::set([
			'__HOMEST__' => '/template/home_temp/'.$this->config['home_temp'].'/',
		],'template.tpl_replace_string');
		$this->n = 0;
		$this->no = 0;
		$this->num = 0;
		$this->nums = 0;
	}
	
	public function index(){
		$category = Db::name('category')->select()->toArray();
		foreach($category as $k=>$vss){
			$category[$k]['id'] = $vss['id'];
			$category[$k]['value'] = $vss['id'];
			$category[$k]['title'] = $vss['title'];
			$category[$k]['name'] = $vss['title'];
		}
		$category = json_encode($category,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
		$mod = Db::name('model')->field('id,name')->select()->toArray();
		foreach($mod as $ke=>$vs){
			$mod[$ke]['id'] = $vs['id'];
			$mod[$ke]['value'] = $vs['id'];
		}
		$mod = json_encode($mod,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
		$matlist = setallmat('10','create_time desc');
		return $this -> view -> fetch('index',['cate'=>$category,'model'=>$mod,'matlist'=>$matlist]);
	}
	/*
		生成内容
	 */
	public function createmater(Request $request){
		if(request()->isAjax()){
			$data = $request->param();
			$matter = [];
			if(!empty($data)){
				/*
				按照模型生成 
				 */
				if(isset($data['mod']) && $data['mod'] == 1){
					if(!empty($data['mods']) || is_string($data['mods'])){
						$tab = Db::name('model')->where('id','in',$data['mods'])->field('tablename')->select()->toArray();
						if(empty($tab)){
							return jsonmsg(0,'当前没有可操作数据');
						}else{
							foreach($tab as $val){
								$mat = Db::name($val['tablename'])->alias('a')->join($val['tablename']."_data b","b.aid=a.id")->field('a.*,b.browse,b.likes,b.comment_t')->where(['a.status'=>0,'a.price'=>1,'a.matpattern'=>2,'a.delete_time'=>NULL])->select()->toArray();
								if(!empty($mat)){
									$matter[] = $mat;
								}
							}
							if(empty($matter)){
								return jsonmsg(0,'当前没有可操作数据');
							}
						}
					}else{
						return jsonmsg(0,'错误的数据');
					}
					
					
				}else if(isset($data['mid']) && $data['mid'] == 1){
					/*
					 按照栏目生成
					 */
					if(!empty($data['mids']) || is_string($data['mids'])){
						$tab = Db::name('model')->field('tablename')->select()->toArray();
						if(empty($tab)){
							return jsonmsg(0,'当前没有可操作数据');
						}else{
							foreach($tab as $val){
								$mat = Db::name($val['tablename'])->alias('a')->join($val['tablename']."_data b","b.aid=a.id")->field('a.*,b.browse,b.likes,b.comment_t')->where(['a.mid'=>$data['mids'],'a.status'=>0,'a.price'=>1,'a.matpattern'=>2,'a.delete_time'=>NULL])->select()->toArray();
								if(!empty($mat)){
									$matter[] = $mat;
								}
							}
							if(empty($matter)){
								return jsonmsg(0,'当前没有可操作数据');
							}
						}
					}
				}else if(isset($data['checkmat']) && $data['checkmat'] == 1){
					/* 
						按照内容id生成
					 */
					if(!empty($data['checkmats']) || is_string($data['checkmats'])){
						$tab = Db::name('model')->field('tablename')->select()->toArray();
						if(empty($tab)){
							return jsonmsg(0,'当前没有可操作数据');
						}else{
							foreach($tab as $val){
								$mat = Db::name($val['tablename'])->alias('a')->join($val['tablename']."_data b","b.aid=a.id")->field('a.*,b.browse,b.likes,b.comment_t')->where('a.id','in',$data['checkmats'])->where(['a.status'=>0,'a.price'=>1,'a.matpattern'=>2,'a.delete_time'=>NULL])->select()->toArray();
								if(!empty($mat)){
									$matter[] = $mat;
								}
							}
							if(empty($matter)){
								return jsonmsg(0,'当前没有可操作数据');
							}
						}
					}
					
				}else if(isset($data['all']) && $data['all'] == 1){
					/*
					 生成全部
					 */
					$tab = Db::name('model')->field('tablename')->select()->toArray();
					if(empty($tab)){
						return jsonmsg(0,'当前没有可操作数据');
					}else{
						foreach($tab as $val){
							$mat = Db::name($val['tablename'])->alias('a')->join($val['tablename']."_data b","b.aid=a.id")->field('a.*,b.browse,b.likes,b.comment_t')->where(['a.status'=>0,'a.price'=>1,'a.matpattern'=>2,'a.delete_time'=>NULL])->select()->toArray();
							if(!empty($mat)){
								$matter[] = $mat;
							}
						}
						if(empty($matter)){
							return jsonmsg(0,'当前没有可操作数据');
						}
					}
				}else{
					return jsonmsg(0,'请选择正确生成方式');
				}
				$matter = ary3_ary2($matter);
				if(!empty($matter)){
					foreach($matter as $key=>$v){
						$cate = Db::name('category')->find($v['mid']);
						if($cate['patternlist'] == 2 || $v['matpattern'] == 2){
							$tb = Db::name('model')->find($cate['modid']);
							$matter[$key]['tabname'] = $tb['id'];
							$matter[$key]['catitle'] = $cate['title'];
							$catinfo['id'] = $cate['id'];
							$catinfo['pid'] = $cate['pid'];
							$member = "";
							if($v["isadmin"] == 0){
							if($v['uid'] > 0 || $v['uid'] !== null){    
							$member = Db::name("member")
							->alias("a")
							->join("member_data b","b.uid=a.id")
							->where("a.id",$v['uid'])
							->fieldRaw("a.name,a.photo,a.email,a.intro,a.home_count,b.comment,b.le_word,b.attention,b.fans,b.contribute")
							->find();
							if($member){
							    $matter[$key]['autro'] = $member['intro'];
							    $matter[$key]['auhct'] = $member['home_count'];
							}
							$matter[$key]['autro'] = "游客投稿";
							$matter[$key]['auhct'] = "**";
							}
							$matter[$key]['autro'] = "游客投稿";
							$matter[$key]['auhct'] = "**";
							}elseif($v["isadmin"] == 1){
							$member = Db::name("admin")
							->alias("a")
							->join("admin_data b","b.uid=a.id")
							->where("a.id",$v['uid'])
							->fieldRaw("a.name,a.photo,a.email,b.comment,b.le_word,b.attention,b.fans,b.contribute")
							->find();
							$matter[$key]['autro'] = $member['name'];
							$matter[$key]['auhct'] = "999+";
							}
							if(!empty($member)){
							$matter[$key]['auname'] = $member['name'];
							$matter[$key]['aupot'] = $member['photo'];
							$matter[$key]['aueml'] = $member['email'];
							$matter[$key]['aucom'] = $member['comment'];
							$matter[$key]['aulrd'] = $member['le_word'];
							$matter[$key]['auaton'] = $member['attention'];
							$matter[$key]['aufans'] = $member['fans'];
							$matter[$key]['auctbe'] = $member['contribute'];
							}else{
							$matter[$key]['auname'] = "未知";
							$matter[$key]['aupot'] = "未知";
							$matter[$key]['aueml'] = "未知";
							$matter[$key]['aucom'] = "未知";
							$matter[$key]['aulrd'] = "未知";
							$matter[$key]['auaton'] = "未知";
							$matter[$key]['aufans'] = "未知";
							$matter[$key]['auctbe'] = "未知";
							}
							/****处理下载地址****/
							$field = Db::name("modfiel")->where(["modid"=>$matter[$key]['tabname'],"forms"=>"down"])->field("field,forms")->find();
							if(!empty($field)){
							$patch = "";
							if(!empty($v[$field["field"]])){
							$patch = explode(',',$v[$field["field"]]);
							$patch = array_chunk($patch,2);
							$matter[$key][$field["field"]] = $patch;
							foreach($matter[$key][$field["field"]] as $ke=>$val){
							    $matter[$key][$field["field"]][$ke]['0'] = $val['0'];
							    $matter[$key][$field["field"]][$ke]['ptname'] = $val['0'];
							    unset($matter[$key][$field["field"]][$ke]['0']);
							    $matter[$key][$field["field"]][$ke]['1'] = $val['1'];
							    $matter[$key][$field["field"]][$ke]['pturl'] = $val['1'];
							    unset($matter[$key][$field["field"]][$ke]['1']);
							}
							}
							}
							/****处理下载地址完毕****/
							/****内容基本参数改写完毕****/
							$prices = ["1"=>"免费","2"=>"付费"];
							$matter[$key]["price"] = $prices[$v["price"]];
							$matter[$key]['isprice'] = 1;
							$html = $this->fetch(Env::get('root_path') . 'template/home_temp/' . $this->config["home_temp"] . '/' . $cate['conttemp'] . '.html',['matcont'=>$matter[$this->n],'catinfo'=>$catinfo])->getContent();
							$html = str_replace('__HOMEST__','/template/home_temp/'.$this->config['home_temp'].'/',$html);
							$html = str_replace('/admin.php/index','',$html);
							$filewhere = Env::get('root_path').$cate['thiswhere'];
							if($cate['pid'] != 0){
								$wheres = $this->foreachwhere($cate["pid"]);
								if($wheres){
									$filewhere = Env::get('root_path').$wheres.'/'.$cate['thiswhere'];
								}
							}
							if(!is_dir($filewhere)){
								@mkdir($filewhere);
							}
							$filename = $this->filenamesrule($cate['filenamesrule'],$v['id'],$cate['thiswhere']);
							$filename = $filename.$cate['filenames'];
							$res = file_put_contents($filewhere.'/'.$filename,$html);
							if($res){
								$this->num++;
							}else{
								$this->nums++;
							}
							$this->n++;
						}else{
							$this->no++;
						}
					}
					return jsonmsg(1,'生成成功['.$this->num.']个,失败['.$this->nums.']个,不是静态模式['.$this->no.']个');
				}else{
					return jsonmsg(0,'当前没有可操作数据');
				}
			}else{
				return jsonmsg(0,'数据空');
			}
		}
		return jsonmsg(0,'错误的请求方式');
	}
	
	/*
		生成栏目
	 */
	public function createlist(Request $request){
		if(request()->isAjax()){
			$data = $request->param();
			if(!empty($data)){
				
				if(isset($data['mod']) && $data['mod'] == 1){
					/*
					 按照模型生成
					 */
					if(!empty($data['mods']) || is_string($data['mods'])){
						$cate = Db::name('category')->where('modid','in',$data['mods'])->select()->toArray();
						if(empty($cate)){
							return jsonmsg(0,'当前没有可操作数据');
						}
					}else{
						return jsonmsg(0,'错误的数据');
					}
				}else if(isset($data['mid']) && $data['mid'] == 1){
					/*
					 按照栏目id生成
					 */
					if(!empty($data['mids']) || is_string($data['mids'])){
						$cate = Db::name('category')->where('id','in',$data['mids'])->select()->toArray();
						if(empty($cate)){
							return jsonmsg(0,'当前没有可操作数据');
						}
					}else{
						return jsonmsg(0,'错误的数据');
					}
				}else if(isset($data['all']) && $data['all'] == 1){
					/*
					 生成全部
					 */
					$cate = Db::name('category')->select()->toArray();
					if(empty($cate)){
						return jsonmsg(0,'当前没有可操作数据');
					}
				}else{
					return jsonmsg(0,'请选择正确生成方式');
				}
				if(!empty($cate)){
					foreach($cate as $key=>$v){
						if($v['patternlist'] == 2){
							$html = $this->fetch(Env::get('root_path') . 'template/home_temp/' . $this->config["home_temp"] . '/' . $v['listtemp'] . '.html',['catinfo'=>$cate[$this->n]])->getContent();
							$html = str_replace('__HOMEST__','/template/home_temp/'.$this->config['home_temp'].'/',$html);
							$html = str_replace('/admin.php/index','',$html);
							$filewhere = Env::get('root_path').$v['thiswhere'];
							if($v['pid'] != 0){
								$wheres = $this->foreachwhere($v["pid"]);
								if($wheres){
									$filewhere = Env::get('root_path').$wheres.'/'.$v['thiswhere'];
								}
							}
							if(!is_dir($filewhere)){
								@mkdir($filewhere);
							}
							$res = file_put_contents($filewhere.'/index.html',$html);
							if($res){
								$this->num++;
							}else{
								$this->nums++;
							}
							$this->n++;
						}else{
							$this->no++;
						}
					}
					return jsonmsg(1,'生成成功['.$this->num.']个,失败['.$this->nums.']个,不是静态模式['.$this->no.']个');
				}else{
					return jsonmsg(0,'当前没有可操作数据');
				}
			}else{
				return jsonmsg(0,'数据空');
			}
		}
		return jsonmsg(0,'错误的请求方式');
	}
}